'use strict';

var services = angular.module('simpleApp.services',
    ['ngResource']);

var url = '/arithmetic/RESTfulServices/calculate';

services.factory('Student', ['$resource',
    function($resource) {
  return $resource(url + '/student/:username',{username: '@username'});
}]);
services.factory('Parent', ['$resource',
    function($resource) {
        return $resource(url + '/parent/:username',{username: '@username'});
    }]);

services.service('calculateService', function(){
   // var operates = new Array(" + ", " - ", " &times; ", " &divide; ");
    //angularjs输出的变量值控制了不转义
    var operates = ['+', '-',String.fromCharCode(215),String.fromCharCode(247)];
    function calculate(type, num_one, num_two) {
        if (type == 0) return parseInt(num_one) + parseInt(num_two);
        else if (type == 1) return parseInt(num_one) - parseInt(num_two);
        else if (type == 2) return parseInt(num_one) * parseInt(num_two);
        else return parseInt(num_one) / parseInt(num_two);
    }
    function randomNumber(max) {
        return Math.floor(Math.random() * max);
    }
    function lcm(m,n){//辗转相除法 求最大公约数
        var u=m,v=n,t=v;
        while(v!=0){
            t=u%v;
            u=v;
            v=t;
        }
        return u;
    }
    this.createEasy = function(){
        var num = randomNumber(4);
        var type = Math.floor(num);
        var operate = operates[type]; // getOperates

        var number_one = Math.floor(randomNumber(100));
        var number_two = Math.floor(randomNumber(100));
        var answer = 0;
        var studentAnswer = "";
        var correctOrWrong = "";

        if (type == 3){
            if(number_two == 0)
                number_two = 1;
            if(number_two > 20)
                number_two = Math.floor(number_two / 2);
            number_one = (Math.round(number_one / number_two) + 1) * number_two;
        }else if(type == 1 && number_one < number_two){
            var number_tmp = number_one;
            number_one = number_two;
            number_two = number_tmp;
        } else if (type == 2 && number_two > 10) number_two = Math.round(number_two / 10);
        answer = calculate(type, number_one, number_two);//4          //5     //6
        return [ number_one,operate, number_two,answer,type,studentAnswer,correctOrWrong];
    };
    this.createHard = function(){
        var count_half;
        var type_one = randomNumber(4);
        var type_two = randomNumber(4);
        var operate_one = operates[type_one];
        var operate_two = operates[type_two];
        var number_one = randomNumber(100);
        var number_two = randomNumber(100);
        var number_three = randomNumber(100);
        var tmp, num;
        var tempResult;
        //whether add parentheses but 50% have the parentheses
        if(((type_one < 2 && type_two > 1) || (type_one > 1 && type_two < 2)) && randomNumber(2) == 0){
            if(type_one < 2){
                if(type_one == 1 && number_one < number_two){
                    tmp = number_one;
                    number_one = number_two;
                    number_two = number_one;
                }
                count_half = calculate(type_one, number_one, number_two);
                if(type_two == 3){
                    if(number_three == 0)
                        number_three = 1;
                    if(number_three > 20)
                        number_three = Math.floor(number_three / 2);
                    num = (Math.round(count_half / number_three) + 1) * number_three - count_half;
                    number_one += num;
                    count_half += num;
                }
                tempResult = ["(" + "   "+ number_one + "   "+   operate_one + "   "+   number_two  + "   "+  ")" +  "   "+  operate_two  + "   "+ number_three,calculate(type_two, count_half, number_three)];
            }else{
                if(type_two == 1 && number_two < number_three){
                    tmp = number_two;
                    number_two = number_three;
                    number_three = tmp;
                }
                count_half = calculate(type_two, number_two, number_three);
                if(type_one == 3){
                    if(count_half == 0){
                        count_half += 1;
                        number_two += 1;
                    }
                    if(count_half > 20){
                        tmp = count_half - Math.floor(count_half / 2);
                        number_two -= tmp;
                        //for fear that the number_two maybe minus number
                        if(number_two < 0){
                            number_three -= 10 - number_two;
                            number_two = 10;
                        }
                        count_half -= tmp;
                    }
                    number_one = (Math.round(number_one / count_half) + 1) * count_half;
                }
                tempResult = [number_one + "   " + operate_one + "   " + "(" + "   "+   number_two  + "   "+  operate_two + "   "+  number_three  + "   "+ ")",calculate(type_one, number_one, count_half)];
            }
        }
        //no parentheses added
        else{
            // all times or divide
            if(type_one * type_two > 3){
                if(type_one == 3){
                    if(number_two == 0)
                        number_two = 1;
                    if(number_two > 20)
                        number_two = Math.floor(number_two / 2);
                    number_one = (Math.round(number_one / number_two) + 1) * number_two;
                }
                count_half = calculate(type_one, number_one, number_two);
                if(type_two == 3){
                    if(number_three == 0)
                        number_three = 1;
                    if(number_three > 20)
                        number_three = Math.floor(number_three / 2);
                    number_one = Math.floor(number_three / lcm(number_three, count_half)) * number_one;
                    count_half = Math.floor(number_three / lcm(number_three, count_half)) * count_half;
                }
                if(type_one == 2 && type_two == 2){
                    number_two = Math.floor(number_two / 10) + 1;
                    number_three = Math.floor(number_three / 10) + 1;
                }
                tempResult = [number_one + "   " + operate_one + "   "+ number_two + "   "+ operate_two + "   " + number_three,calculate(type_two, count_half, number_three)];
            }
            else if(type_one < 2 && type_two < 2){
                if(type_one == 1 && number_one < number_two){
                    tmp = number_one;
                    number_one = number_two;
                    number_two = tmp;
                }
                count_half = calculate(type_one, number_one, number_two);
                if(type_two == 1 && number_three > count_half){
                    tempResult =[ number_three + "   "+ operate_two + "   "+ "(" + "   "+ number_one + "   "+ operate_one + "   "+ number_two + "   "+ ")",number_three - count_half];
                }
                tempResult =[ number_one + "   "+ operate_one + "   "+ number_two + "   "+ operate_two+ "   " + number_three,calculate(type_two, count_half, number_three)];
            }else{
                if(type_one < 2){
                    if(type_two == 3){
                        if(number_three == 0)
                            number_three = 1;
                        if(number_three > 20)
                            number_three = Math.floor(number_three / 2);
                        number_two = (Math.floor(number_two / number_three) + 1) * number_three;
                    }
                    count_half = calculate(type_two, number_two, number_three);
                    if(type_one == 1 && number_one < count_half){
                        tempResult = [ number_two + operate_two + number_three + operate_one + number_one,count_half - number_one];
                    }
                    tempResult = [number_one+ "   " + operate_one+ "   " + number_two + "   "+ operate_two + "   "+ number_three,calculate(type_one, number_one, count_half)]
                }else{
                    if(type_one == 3){
                        if(number_two == 0)
                            number_two = 1;
                        if(number_two > 20)
                            number_two = Math.floor(number_two / 2);
                        number_one = (Math.floor(number_one / number_two) + 1) * number_two;
                    }
                    count_half = calculate(type_one, number_one, number_two);
                    if(type_two == 1 && count_half < number_three){
                      tempResult =  [number_three + operate_two + number_one + operate_one + number_two, number_three - count_half] ;
                    }
                    tempResult = [number_one + "   "+ operate_one + "   "+ number_two + "   "+ operate_two + "   "+ number_three,calculate(type_two, count_half, number_three)];
                }
            }
        }
        return tempResult;
    };
});
